/**
 * <p>Title: EntityManager.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: xunjie</p>
 * @author shenhao
 * @date 2010-3-9
 * @version 1.0
 */
package cn.haona.game.simb.object.god;

import java.util.Date;
import java.util.HashMap;

import cn.haona.game.simb.comm.util.IllWordFilter;
import cn.haona.game.simb.object.Cell;
import cn.haona.game.simb.object.bio.Human;
import cn.haona.game.simb.object.bio.User;
import cn.haona.game.simb.object.department.HAB;
import cn.haona.game.simb.object.geo.World;
import cn.haona.game.simb.object.god.cache.Cache;
import cn.haona.reflect.ReflectionBasic;

/**
 * <p>
 * Title: EntityManager
 * </p>
 * <p>
 * Description: 实体管理器
 * </p>
 * <p>
 * Company: xunjie
 * </p>
 * 
 * @author shenhao
 * @date 2010-3-9
 */
public class EntityManager implements Manager
{

	// 初始化世界-------初始化到街道为止
	private World world;

	// 所有CLASS保存HashMap
	private HashMap<String, Class> hmClasses;

	// 民政局对象
	private HAB hab;

	// 本地词语过滤器
	private IllWordFilter illWordFilterLocal;

	/**
	 * 得到实体在缓存中的Key
	 * 
	 * @param className
	 * @param id
	 * @return
	 */
	public static String getCacheKey(String className, long id)
	{
		if (className != null)
		{
			return className + "_" + id;
		}
		return null;
	}

	/**
	 * 实体在缓存总的过期时间
	 * 
	 * @return
	 */
	public static Date getCacheExpire()
	{
		return new Date(System.currentTimeMillis() + 1200000);
	}

	public IllWordFilter getIllWordFilterLocal()
	{
		return illWordFilterLocal;
	}

	public void setIllWordFilterLocal(IllWordFilter illWordFilterLocal)
	{
		this.illWordFilterLocal = illWordFilterLocal;
	}

	/**
	 * 
	 */
	public EntityManager()
	{
		initMe();
	}

	/*
	 * (non-Javadoc) <p>Title: initMe</p> <p>Description: </p>
	 * 
	 * @see cn.haona.game.simb.god.Manager#initMe()
	 */
	public void initMe()
	{
		// TODO Auto-generated method stub
		hmClasses = new HashMap<String, Class>();
	}

	/*
	 * (non-Javadoc) <p>Title: destoryMe</p> <p>Description: </p>
	 * 
	 * @see cn.haona.game.simb.god.Manager#destoryMe()
	 */
	public void destoryMe()
	{
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc) <p>Title: getID</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 * @see cn.haona.game.simb.god.Manager#getID()
	 */
	public String getID()
	{
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 得到单元的实例对象并初始化PO
	 * 
	 * @param className
	 * @param id
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public Cell getCellInstance(String className, long id) throws ClassNotFoundException, InstantiationException,
			IllegalAccessException
	{

		// 返回的单元
		Cell cell = null;
		// 先看缓存中是否有该对象
		// 先从缓存中读取
		Cache cache = MainManager.getInstance().getCacheManager().getRemoteCache();
//		cell = (Cell) cache.get(EntityManager.getCacheKey(className, id));
		// 如果缓存中也没有
		if (cell == null)
		{

			// 从本地缓存中得到模板
			Class c = hmClasses.get(className);
			// 如果没有模板那么从加载器里得到一个
			if (c == null)
			{
				// System.out.println("-------s--------------");
				c = Class.forName(className);
				// c = ClassLoader.getSystemClassLoader().loadClass(className);
				// System.out.println("-------e--------------");
				if (c != null)
				{
					// 保存模板到缓存中
					hmClasses.put(className, c);
				}
			}

			if (c != null)
			{
				cell = (Cell) c.newInstance();
				cell.initMe(id);
			}
			//存入缓存
			cache.set(EntityManager.getCacheKey(className, id), cell, EntityManager.getCacheExpire());
		}
//		//缓存中的
//		else
//		{
//			//得到缓存对象中的持久化对象
//			if(cell.getPO()!=null)
//			{
//			}
//		}


		return cell;

	}

	public HAB getHab()
	{
		return hab;
	}

	public void setHab(HAB hab)
	{
		this.hab = hab;
	}

}
